package egar

import (
	"fmt"
	"log"
	"os"
)

type GameConfig struct {
	ServerName           string
	ServerHost           string
	ServerPort           int
	ServerMaxConnections int

	GameTimeStep  int
	GameMapWidth  float64
	GameMapHeight float64

	PlayerMaxCells int

	FoodMinMass float64
	FoodMaxMass float64
	FoodTotal   int

	VirusMinMass float64
	VirusMaxMass float64
	VirusTotal   int

	SporeMass float64

	CellInitMass       float64
	CellMinMassToVomit float64
	CellMinMassToSplit float64
	CellVomitInterval  int
	CellSplitInterval  int

	OtherBoundaryCollision    bool
	OtherMinOverlapToEat      float64
	OtherMinMassMultipleToEat float64
}

var globalConfig *GameConfig = nil

func loadGameConfig(filename string) *GameConfig {
	config := &GameConfig{}

	jsonData, err := os.ReadFile(filename)
	if err != nil {
		panic(fmt.Errorf("the configuration file does not exist: %s", filename))
	}

	p := NewJsonParser()

	jsonRoot, err := p.Parse(jsonData)
	if err != nil {
		panic(fmt.Errorf("the configuration file parse error: %s\nmessage: %s", filename, err))
	}

	config.ServerName = jsonRoot.GetAttribute("server").GetAttribute("name").String()
	config.ServerHost = jsonRoot.GetAttribute("server").GetAttribute("host").String()
	config.ServerPort = jsonRoot.GetAttribute("server").GetAttribute("port").Int()
	config.ServerMaxConnections = jsonRoot.GetAttribute("server").GetAttribute("maxConnections").Int()

	config.GameTimeStep = jsonRoot.GetAttribute("game").GetAttribute("timeStep").Int()
	config.GameMapWidth = jsonRoot.GetAttribute("game").GetAttribute("mapWidth").Number()
	config.GameMapHeight = jsonRoot.GetAttribute("game").GetAttribute("mapHeight").Number()

	config.PlayerMaxCells = jsonRoot.GetAttribute("player").GetAttribute("maxCells").Int()

	config.FoodMinMass = jsonRoot.GetAttribute("food").GetAttribute("minMass").Number()
	config.FoodMaxMass = jsonRoot.GetAttribute("food").GetAttribute("maxMass").Number()
	config.FoodTotal = jsonRoot.GetAttribute("food").GetAttribute("total").Int()

	config.VirusMinMass = jsonRoot.GetAttribute("virus").GetAttribute("minMass").Number()
	config.VirusMaxMass = jsonRoot.GetAttribute("virus").GetAttribute("maxMass").Number()
	config.VirusTotal = jsonRoot.GetAttribute("virus").GetAttribute("total").Int()

	config.SporeMass = jsonRoot.GetAttribute("spore").GetAttribute("mass").Number()

	config.CellInitMass = jsonRoot.GetAttribute("cell").GetAttribute("initMass").Number()
	config.CellMinMassToVomit = jsonRoot.GetAttribute("cell").GetAttribute("minMassToVomit").Number()
	config.CellMinMassToSplit = jsonRoot.GetAttribute("cell").GetAttribute("minMassToSplit").Number()
	config.CellVomitInterval = jsonRoot.GetAttribute("cell").GetAttribute("vomitInterval").Int()
	config.CellSplitInterval = jsonRoot.GetAttribute("cell").GetAttribute("splitInterval").Int()

	config.OtherBoundaryCollision = jsonRoot.GetAttribute("other").GetAttribute("boundaryCollision").Bool()
	config.OtherMinOverlapToEat = jsonRoot.GetAttribute("other").GetAttribute("minOverlapToEat").Number()
	config.OtherMinMassMultipleToEat = jsonRoot.GetAttribute("other").GetAttribute("minMassMultipleToEat").Number()

	log.Println("load configuration: ", p.Format(jsonRoot))

	globalConfig = config

	return config
}
